<template>
  <el-dialog
    v-model="visible"
    :title="!dataForm.areaId ? $t('sysManagement.add') : $t('sysManagement.modify')"
    :close-on-click-modal="false"
  >
    <el-form
      ref="dataFormRef"
      :model="dataForm"
      :rules="dataRule"
      label-width="100px"
      @submit.prevent
      @keyup.enter="onSubmit(dataFormRef)"
    >
      <el-form-item
        :label="$t('sys.areaName')"
        prop="areaName"
      >
        <el-input v-model="dataForm.areaName" />
      </el-form-item>
      <el-form-item
        :label="$t('sys.superiorAreaL')"
        prop="parentId"
      >
        <el-cascader
          model-value="selectedOptions"
          :options="areaList"
          :props="categoryTreeProps"
          clearable
          filterable
          @change="handleChange"
        />
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <div
          class="default-btn"
          @click="visible = false"
        >
          {{ $t('coupon.cancel') }}
        </div>
        <div
          class="default-btn primary-btn"
          @click="onSubmit(dataFormRef)"
        >
          {{ $t('coupon.confirm') }}
        </div>
      </div>
    </template>
  </el-dialog>
</template>

<script setup>
import { ElMessage } from 'element-plus'
import { treeDataTranslate } from '@/utils'

// 父组件传递的方法
const emit = defineEmits(['refreshDataList'])

const dataFormRef = ref(null)
const visible = ref(false)
const dataForm = ref({
  areaId: '',
  areaName: null,
  parentId: null,
  level: null
})
const page = reactive({
  total: 0, // 总页数
  currentPage: 1, // 当前页数
  pageSize: 20 // 每页显示多少条
})
const dataRule = reactive({
  areaName: [{ required: true, message: $t('sysManagement.areaTips'), trigger: 'blur' }]
})
const isSubmit = ref(false)
const areaList = ref([])
const categoryTreeProps = reactive({
  expandTrigger: 'hover', // 次级菜单的展开方式
  checkStrictly: true, // 是否严格的遵守父子节点不互相关联
  value: 'areaId',
  label: 'areaName'
})
const selectedOptions = ref('')

// 表单提交
const onSubmit = async formEl => {
  if (!formEl) return
  await formEl.validate(valid => {
    if (!valid) return
    if (!dataForm.value.parentId) {
      dataForm.value.parentId = 0
      dataForm.value.level = 0
    }
    if (isSubmit.value) {
      return false
    }
    isSubmit.value = true
    http({
      url: http.adornUrl('/admin/area'),
      method: dataForm.value.areaId ? 'put' : 'post',
      data: http.adornData(dataForm.value)
    }).then(() => {
      ElMessage({
        message: $t('publics.operation'),
        type: 'success',
        duration: 1500,
        onClose: () => {
          visible.value = false
          emit('refreshDataList', page)
        }
      })
    }).catch(() => {
      isSubmit.value = false
    })
  })
}

const handleChange = val => {
  if (!val) return
  dataForm.value.parentId = val[val.length - 1]
  dataForm.value.level = val.length
}

const init = areaId => {
  dataForm.value.areaId = areaId || 0
  visible.value = true
  isSubmit.value = false
  selectedOptions.value = ''
  nextTick(() => {
    dataFormRef.value.resetFields()
    if (dataForm.value.areaId) {
      http({
        url: http.adornUrl('/admin/area/info/' + dataForm.value.areaId),
        method: 'get',
        params: http.adornParams()
      }).then(({ data }) => {
        dataForm.value = data
        selectedOptions.value = dataForm.value.parentId
        categoryTreeProps.areaId = dataForm.value.areaId
        categoryTreeProps.areaName = dataForm.value.areaName
      })
    }
    http({
      url: http.adornUrl('/admin/area/list'),
      method: 'get',
      params: http.adornParams({
        maxLevel: 2
      })
    }).then(({ data }) => {
      areaList.value = treeDataTranslate(data, 'areaId', 'parentId')
    })
  })
}
defineExpose({ init })
</script>
